package cn.iocoder.yudao.module.picturebook.service.picturepost;

import cn.iocoder.yudao.module.picturebook.dal.mysql.comment.CommentMapper;
import cn.iocoder.yudao.module.picturebook.dal.mysql.picturepost.PicturePostMapper;
import cn.iocoder.yudao.module.picturebook.dal.mysql.postlike.PostLikeMapper;
import cn.iocoder.yudao.module.picturebook.dal.mysql.user.PBUserMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;

import java.util.*;

import cn.iocoder.yudao.module.picturebook.controller.admin.picturepost.vo.*;
import cn.iocoder.yudao.module.picturebook.dal.dataobject.picturepost.PicturePostDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;


import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.picturebook.enums.ErrorCodeConstants.*;

/**
 * 帖子 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class PicturePostServiceImpl implements PicturePostService {

    @Resource
    private PicturePostMapper picturePostMapper;
    @Resource
    private PBUserMapper pBUserMapper;
    @Resource
    private PostLikeMapper postLikeMapper;
    @Resource
    private CommentMapper commentMapper;

    @Override
    public Long createPost(PicturePostSaveReqVO createReqVO, Long userId) {
        // 插入
        PicturePostDO post = BeanUtils.toBean(createReqVO, PicturePostDO.class);
        post.setCreator(String.valueOf(userId));
        picturePostMapper.insert(post);

        // 返回
        return post.getPostId();
    }

    @Override
    public void updatePost(PicturePostSaveReqVO updateReqVO) {
        // 校验存在
        validatePostExists(updateReqVO.getPostId());
        // 更新
        PicturePostDO updateObj = BeanUtils.toBean(updateReqVO, PicturePostDO.class);
        picturePostMapper.updateById(updateObj);
    }

    @Override
    public void deletePost(Long id) {
        // 校验存在
        validatePostExists(id);
        // 删除
        picturePostMapper.deleteById(id);
    }

    @Override
    public void deletePostListByIds(List<Long> ids) {
        // 删除
        picturePostMapper.deleteByIds(ids);
    }


    private void validatePostExists(Long id) {
        if (picturePostMapper.selectById(id) == null) {
            throw exception(POST_NOT_EXISTS);
        }
    }

    @Override
    public PicturePostDO getPost(Long id) {
        return picturePostMapper.selectById(id);
    }

    @Override
    public PageResult<PicturePostRespVO> getPostPage(PicturePostPageReqVO pageReqVO, Long userId) {
        PageResult<PicturePostDO> picturePostDOPageResult = picturePostMapper.selectPage(pageReqVO);
        PageResult<PicturePostRespVO> postRespVOPageResult = BeanUtils.toBean(picturePostDOPageResult, PicturePostRespVO.class);
        for (PicturePostRespVO postRespVO : postRespVOPageResult.getList()) {
            //获取头像
            postRespVO.setAvatar(pBUserMapper.selectById(postRespVO.getCreator()).getAvatar());
            //获取昵称
            postRespVO.setCreator(pBUserMapper.selectById(postRespVO.getCreator()).getRealName());
            //获取点赞数量
            postRespVO.setLikeCount(postLikeMapper.getLikeCount(postRespVO.getPostId()));
            //获取评论数
            postRespVO.setCommentCount(commentMapper.getCommentCount(postRespVO.getPostId()));
            //获取是否点赞
            postLikeMapper.isLike(postRespVO.getPostId(), String.valueOf(userId));
            if (postLikeMapper.isLike(postRespVO.getPostId(), String.valueOf(userId)) != null) {
                postRespVO.setIsLike(postLikeMapper.isLike(postRespVO.getPostId(), String.valueOf(userId)));
            } else postRespVO.setIsLike(0);
        }
        return postRespVOPageResult;
    }

}